/**
 * Description:
 * Class Name:MultiThreadDownLoad
 * Date:2022/5/20 20:04
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第17章_网络编程._17_2_Java的基本网络支持._17_2_3_URL_URLConnection和URLPermission;

public class MultiThreadDownLoad {
    public static void main(String[] args) throws Exception{
        String targetURL = "http://tiebapic.baidu.com/forum/w%3D580/sign=5eceb2ee0e36acaf59e096f44cd88d03/25f88219ebc4b745d68846c18afc1e178b821572.jpg?tbpicau=2022-05-22-05_608c4ee427137d02faaef6d131a9d51f";
        // 初始化下载器对象
        final var DownLoadUtil = new DownLoadUtil(targetURL, "Chapter_17.jpg", 4);
        // 开始下载，这个方法里面会自己启动相应数量的线程来开始下载
        DownLoadUtil.download();
        // 另外启动一个线程来查看目前的进度
        new Thread(()->{
            while (DownLoadUtil.getCompleteRate() < 1){
                // 每隔0.1秒查询一个任务的完成进度
                System.out.println("已完成：" + DownLoadUtil.getCompleteRate());
                try {
                    Thread.sleep(100);
                }
                catch (Exception e){

                }
            }
        }).start();
    }
}
